from pyglet import gl
import pyglet

# Groupes de texture
class TextureEnableGroup(pyglet.graphics.Group):
    def set_state(self):
        gl.glEnable(gl.GL_TEXTURE_2D)

    def unset_state(self):
        gl.glDisable(gl.GL_TEXTURE_2D)
        
texture_enable_group = TextureEnableGroup()

class TextureBindGroup(pyglet.graphics.Group):
    def __init__(self, texture, parent=None):
        super(TextureBindGroup, self).__init__(parent=texture_enable_group)
        self._texture = texture

    def set_state(self):
        gl.glBindTexture(self._texture.target, self._texture.id)

class TransformGroup(pyglet.graphics.Group):
    def __init__(self, parent, transform_matrix):
        super(TransformGroup, self).__init__(parent=parent)
        self._transform = transform_matrix
    
    def set_state(self):
        """
        sets shader model view
        """
        
        gl.glPushMatrix()
        gl.glMultMatrixf(self._transform)
        

    def unset_state(self):
        """
        sets shader model view
        """
        
        gl.glPopMatrix()

def pack_transform(matrix):
    elems = matrix[:]
    return (gl.GLfloat * len(elems))(*elems)
        